package com.skcc.ts.nsi.user;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import com.skcc.ts.nsi.R;
import com.skcc.ts.nsi.service.SocketClientService;

public class MEUsrPrjRegist extends Activity {

	private Button btn_usr_prj_regi_signup;
	private TextView usr_name;
	private TextView usr_id;
	private String[] pjtCode;
	private String[] pjtName;
	private String[] role = { "-- 선택 --", "PM", "PL", "DEV" };
	private String myName;
	private String myId;
	private boolean terminate;
	private String selectedPjtName;
	private String selectedPjtCode;
	private String selectedRole;
	private String addr;
	private String port;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.meusrprjregi);

		usr_name = (TextView) findViewById(R.id.textView_team_usr_name);
		usr_id = (TextView) findViewById(R.id.textView_team_usr_id);

		setInfo();// 등록정보세팅
		initSpinners();// 스피너 동적생성
		initButtons();// 버튼 세팅
	}

	private void setInfo() {
		Intent receiveI = getIntent();
		boolean doYN = receiveI.getBooleanExtra("start", false);
		if (doYN) {
			myId = receiveI.getStringExtra("myId");
			myName = receiveI.getStringExtra("myName");
			terminate = true;
			String[] code = receiveI.getStringArrayExtra("projectCode");
			String[] name = receiveI.getStringArrayExtra("projectName");
			addr = receiveI.getStringExtra("loginAddr");
			port = receiveI.getStringExtra("loginPort");
			ArrayList<String> transCode = new ArrayList<String>();
			ArrayList<String> transName = new ArrayList<String>();
			transCode.add("-- 선택 --");
			transName.add("-- 선택 --");
			for (int i = 0; i < code.length; i++) {
				transCode.add(code[i]);
				transName.add(name[i]);
			}
			pjtCode = (String[]) transCode.toArray(new String[(code.length) + 1]);
			pjtName = (String[]) transName.toArray(new String[(code.length) + 1]);
			usr_name.setText(myName);
			usr_name.setGravity(Gravity.CENTER);
			usr_name.setTextSize(20);
			usr_name.setTextColor(Color.BLACK);
			usr_id.setText(myId);
			usr_id.setGravity(Gravity.CENTER);
			usr_id.setTextSize(20);
			usr_id.setTextColor(Color.BLACK);
		}
	}

	private void initSpinners() {
		ArrayAdapter<String> adapter_pjt = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, pjtName);
		ArrayAdapter<String> adapter_role = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, role);

		Spinner sp_pjt = (Spinner) findViewById(R.id.spinner_usr_prj_regi_list_1);
		adapter_pjt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sp_pjt.setAdapter(adapter_pjt);

		sp_pjt.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				selectedPjtName = pjtName[position];
				selectedPjtCode = pjtCode[position];
			}

			public void onNothingSelected(AdapterView<?> parent) {
				selectedPjtName = "-- 선택 --";
			}
		});

		Spinner sp_role = (Spinner) findViewById(R.id.spinner_usr_prj_regi_list_4);
		adapter_role.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sp_role.setAdapter(adapter_role);

		sp_role.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				selectedRole = role[position];
			}

			public void onNothingSelected(AdapterView<?> parent) {
				selectedRole = "-- 선택 --";
			}
		});
	}

	private void initButtons() {
		btn_usr_prj_regi_signup = (Button) findViewById(R.id.btn_usr_prj_regi_signup);

		btn_usr_prj_regi_signup.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				if ("-- 선택 --".equals(selectedPjtCode)) {
					new AlertDialog.Builder(MEUsrPrjRegist.this).setTitle("알림").setMessage("프로젝트를 선택하여 주십시오").setPositiveButton("OK", null)
							.show();
				} else if ("-- 선택 --".equals(selectedRole)) {
					new AlertDialog.Builder(MEUsrPrjRegist.this).setTitle("알림").setMessage("역할을 지정하여 주십시오").setPositiveButton("OK", null)
							.show();
				} else {
					Intent sendI = new Intent(MEUsrPrjRegist.this, SocketClientService.class);
					sendI.putExtra("id", 15);// 프로젝트 등록
					sendI.putExtra("userId", usr_id.getText().toString());
					sendI.putExtra("regiPjtCode", selectedPjtCode);
					sendI.putExtra("userRole", selectedRole);
					sendI.putExtra("loginAddr", addr);
					sendI.putExtra("loginPort", port);
					sendI.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
					Log.d("id", usr_id.getText().toString());
					Log.d("id", selectedPjtCode);
					Log.d("id", selectedRole);
					startService(sendI);
					if (terminate) {
						finish();
					}
				}
			}
		});
	}

}